package com.uyaer.engine.display
{
	import com.uyaer.engine.Engine;
	
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.net.URLRequest;

	public class Scene
	{
		public var maxWidth:int; //加载背景后最大
		public var maxTop:int = 255;
		public var stage:Stage;
		public var gameContainer:Sprite;
		public var background:Sprite;  //背景层
		public var bg1:Sprite; //远景
		public var bg2:Sprite; //中景
		public var bg3:Sprite; //近景
		public var personCon:Sprite;   //人物层
		public var otherCon:Sprite;    //遮挡人物层
		public var bgLayer:Sprite; //背景人物层
		public var fixedLayer:Sprite; //聊天菜单
		public var panelLayer:Sprite; //面板层
		public var backLayer:Sprite; //备用层
		public var alertLayer:Sprite; //警告层
		public var tipLayer:Sprite; //提示层
		
		private var resArr:Array; //需要加载的资源
		private var loader:Loader;
		
		private var img_bg1:Bitmap;
		private var img_bg2:Bitmap;
		private var img_bg3:Bitmap;
		public function Scene(stage:Stage)
		{
			this.stage = stage;
			
			gameContainer = new Sprite();
			stage.addChild(gameContainer);
			
			bgLayer = new Sprite();
			bgLayer.name = "bgLayer";
			gameContainer.addChild(bgLayer);
				background = new Sprite();
				background.name = "background";
				bgLayer.addChild(background);
					bg1 = new Sprite();
					bg1.name = "bg1";
					background.addChild(bg1);
					bg2 = new Sprite();
					bg2.name = "bg2";
					background.addChild(bg2);
					bg3 = new Sprite();
					bg3.name = "bg3";
					background.addChild(bg3);
				personCon = new Sprite();
				personCon.name = "personCon";
				bgLayer.addChild(personCon);
				otherCon = new Sprite();
				otherCon.name = "otherCon";
				bgLayer.addChild(otherCon);
			fixedLayer = new Sprite();
			fixedLayer.name = "fixedLayer";
			gameContainer.addChild(fixedLayer);
			panelLayer = new Sprite();
			panelLayer.name = "panelLayer";
			gameContainer.addChild(panelLayer);
			backLayer = new Sprite();
			backLayer.name = "backLayer";
			gameContainer.addChild(backLayer);
			alertLayer = new Sprite();
			alertLayer.name = "alertLayer";
			gameContainer.addChild(alertLayer);
			tipLayer = new Sprite();
			tipLayer.name = "tipLayer";
			gameContainer.addChild(tipLayer);
		}
		
		public function resetPostion(x:int , y:int):void{
			gameContainer.x = x;
			gameContainer.y = y;
		}
		
		public function changeScene(res:Array):void{
			resArr = res;
			if(!resArr){
				resArr = [];
			}
			loadRes();
		}
		
		private function loadRes():void{
			if(!loader){
				loader = new Loader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderOver);
			}
			if(resArr.length > 0){
				loader.load(new URLRequest(resArr.pop()));
			}else{
				loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,loaderOver);
				loadBackground();
			}
		}
		
		private function loadBackground():void{
			if(!img_bg3){
				img_bg3 = new Bitmap();
				bg3.addChild(img_bg3);
			}
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE,bgLoadOver);
			loader.load(new URLRequest("res/bg3.jpg"));
		}
		
		private function bgLoadOver(e:Event):void{
			img_bg3.bitmapData = (loader.contentLoaderInfo.content as Bitmap).bitmapData.clone();
			img_bg3.y = Camera.HEIGHT - img_bg3.height;
			maxWidth = img_bg3.width;
		}
		
		private function loaderOver(e:Event):void{
			loader.unload();
			loadRes();
		}
	}
}